package com.it.you.zookeeper.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.data.Stat;
import org.junit.jupiter.api.Test;

/**
 * 2
 * curator基础用法
 */
public class CuratorBasic extends CuratorTest {

    /**
     * 创建节点
     */
    @Test
    public void createNode() {
        try {
            client.create().creatingParentsIfNeeded().forPath("/zookeeper/nodeTest", "hello".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改节点数据
     */
    @Test
    public void modifyData() {
        try {
            Stat stat = client.setData().forPath("/zookeeper/nodeTest", "hello1".getBytes());
            System.out.println(stat);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取节点数据
     */
    @Test
    public void getData() {
        try {
            byte[] bytes = client.getData().forPath("/zookeeper/nodeTest");
            System.out.println(new String(bytes));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取子节点
     */
    @Test
    public void getChild() {
        try {
            Object object = client.getChildren().forPath("/zookeeper");
            System.out.println(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建临时节点
     */
    @Test
    public void ephemeralNode() throws Exception {
        client.create().withMode(CreateMode.EPHEMERAL).forPath("/zookeeper/EphemeralNode");
        Thread.sleep(Integer.MAX_VALUE);
    }

    /**
     * 循环创建临时有序节点
     * @throws Exception
     */
    @Test
    public void ehoemeralSequentialNode() throws Exception {
        for (int i = 0; i < 10; i++) {
            client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
                    .forPath("/zookeeper/ehoemeralSequentialNode/h" + i);
        }
        Thread.sleep(Integer.MAX_VALUE);
    }


    /**
     * 节点监控
     * @throws Exception
     */
    public void watchDemo() throws Exception {
        //监听子节点
        client.getChildren().watched().forPath("1");

        //监听数据变化
        client.getData().usingWatcher(new CuratorWatcher() {
            @Override
            public void process(WatchedEvent event) throws Exception {
                switch (event.getType()) {
                    case NodeDataChanged:

                }
            }
        }).forPath(":");

        //监听子节点变化
        PathChildrenCache cache = new PathChildrenCache(client, "", true);
        cache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event)
                    throws Exception {
                switch (event.getType()) {
                    case CHILD_ADDED:
                        break;
                    case CHILD_UPDATED:
                    case CHILD_REMOVED:
                }
            }
        });
    }

}
